<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.interfaces.suntai.utils.InvokeHelper" %>
<%@ page import="weaver.interfaces.suntai.utils.SunTaiUtil" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="baseBean" class="weaver.general.BaseBean"/>
<%
    JSONObject returnJson = new JSONObject();
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();
    int billid = 291;
    String tablename = "uf_xshttz";
    String ids = Util.null2String( request.getParameter( "ids" ) );
    String fields = "id,htbh,htmc,htmj,htje,bzwb,hl,qdsj," +
            "kssj,jssj,khmcwb,khdm,htjfflwb,wfqddwmc,wfqddwdm,htbz,sdbkwb,hxywflwb," +
            "sqrwb,sfcdxqht,zgjfjgwb,cplxwb,zzlxwb,zylywb,sfcdzxwb,kjcxflwb,htxzflwb,jssywb," +
            "jssyewb,xyflwb,htlbwb,jpjfflwb,htzt";
    rs.executeQuery( "select " + fields + " from " + tablename + " where id in (" + ids + ")" );
    int counts = rs.getCounts();
    int addFailureCount = 0;
    int addSuccessCount = 0;
    int changeFailureCount = 0;
    int changeSuccessCount = 0;
    while (rs.next()) {
        JSONObject jsonData = new JSONObject();
        String mainid = Util.null2String( rs.getString( "id" ) );
        jsonData.put( "companyCode", "01-0070-202202-001" );
        jsonData.put( "contractCode", Util.null2String( rs.getString( "htbh" ) ) );
        jsonData.put( "contractName", Util.null2String( rs.getString( "htmc" ) ) );
        jsonData.put( "contractSecretLev", Util.null2String( rs.getString( "htmj" ) ));
        jsonData.put( "contractAmount", Util.null2String( rs.getString( "htje" ) ) );
        jsonData.put( "currency", Util.null2String( rs.getString( "bzwb" ) ) );
        jsonData.put( "exchangeRate", Util.null2String( rs.getString( "hl" ) ) );
        jsonData.put( "signTime", Util.null2String( rs.getString( "qdsj" ) ) );
        jsonData.put( "startTime", Util.null2String( rs.getString( "kssj" ) ) );
        jsonData.put( "endTime", Util.null2String( rs.getString( "jssj" ) ) );
        jsonData.put( "contractOwner", Util.null2String( rs.getString( "khmcwb" ) ) );
        jsonData.put( "ownerUnifyCode", Util.null2String( rs.getString( "khdm" ) ) );
        jsonData.put( "contractPartyB", Util.null2String( rs.getString( "wfqddwmc" ) ) );
        jsonData.put( "partyBUnifyCode", Util.null2String( rs.getString( "wfqddwdm" ) ) );
        jsonData.put( "contractRemark", Util.null2String( rs.getString( "htbz" ) ) );
        jsonData.put( "fourPlate", Util.null2String( rs.getString( "sdbkwb" ) ) );
        String htlbwb = Util.null2String( rs.getString( "htlbwb" ) );
        jsonData.put( "theirBusiness",htlbwb);
        String jpjfflwb = Util.null2String( rs.getString( "jpjfflwb" ) );
        String htjfflwb = Util.null2String( rs.getString( "htjfflwb" ) );
        jsonData.put( "ownerClassify","JPHT".equals( htlbwb )?jpjfflwb:htjfflwb);
        jsonData.put( "coreBusinessClass", Util.null2String( rs.getString( "hxywflwb" ) ) );
        jsonData.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );
        jsonData.put( "tenantCode", "ZDKST" );
        jsonData.put( "status", "rkz");
        String sfcdxqht = Util.null2String( rs.getString( "sfcdxqht" ) );
        jsonData.put( "isGreatNewCont", "0".equals( sfcdxqht ) ? "Y" : "N" );
        jsonData.put( "armyClass2", "JPHT".equals( htlbwb )?htjfflwb:jpjfflwb);
        jsonData.put( "chargeArmyOffice", Util.null2String( rs.getString( "zgjfjgwb" ) ) );
        jsonData.put( "productType", Util.null2String( rs.getString( "cplxwb" ) ) );
        jsonData.put( "makeType", Util.null2String( rs.getString( "zzlxwb" ) ) );
        jsonData.put( "professionalField", Util.null2String( rs.getString( "zylywb" ) ) );
        jsonData.put( "isGreatProject", Util.null2String( rs.getString( "sfcdzxwb" )));
        jsonData.put( "sciTechClassify", Util.null2String( rs.getString( "kjcxflwb" ) ) );
        jsonData.put( "techEarnings", Util.null2String( rs.getString( "htxzflwb" ) ) );
        jsonData.put( "technicalB", Util.null2String( rs.getString( "jssywb" ) ) );
        jsonData.put( "technicalB2", Util.null2String( rs.getString( "jssyewb" ) ) );
        jsonData.put( "industryClassify", Util.null2String( rs.getString( "xyflwb" ) ) );

        //明细1
        JSONArray progressArray = new JSONArray();
        String fields_dt1 = "sjskdsxhtj,jhskdrq,hskdje";
        rs.executeQuery( "select " + fields_dt1 + " from " + tablename + "_dt1 where mainid=?", mainid );
        while (rs.next()) {
            JSONObject progressjson = new JSONObject();
            progressjson.put( "progressItems", Util.null2String( rs.getString( "sjskdsxhtj" ) ) );
            progressjson.put( "progressTimes", Util.null2String( rs.getString( "jhskdrq" ) ) );
            progressjson.put( "receivableAmount", Util.null2String( rs.getString( "hskdje" ) ) );
            progressArray.add( progressjson );
        }
        jsonData.put( "progress", progressArray );

        //明细2
        JSONArray productsArray = new JSONArray();
        rs.executeQuery( "select cpmc,cpxh,cpsl,cpdwwb,jldj from " + tablename + "_dt2 where mainid=?", mainid );
        while (rs.next()) {
            JSONObject productsjson = new JSONObject();
            productsjson.put( "productName", Util.null2String( rs.getString( "cpmc" ) ) );//产品名称
            String cpxh = Util.null2String( rs.getString( "cpxh" ) );
            productsjson.put( "productModel", SunTaiUtil.getSelectItem( billid, "cpxh", cpxh ) );//产品型号
            productsjson.put( "productCount", Util.null2String( rs.getString( "cpsl" ) ) );//产品数量
            productsjson.put( "productPrice", Util.null2String( rs.getString( "jldj" ) ) );//计量单价
            productsjson.put( "productUnit", Util.null2String( rs.getString( "cpdwwb" ) ) );//产品单位
            productsArray.add( productsjson );
        }
        jsonData.put( "products", productsArray );
        //新增销售合同
        String resultText = InvokeHelper.addSellcont( jsonData.toJSONString() );
        JSONObject resultJson = JSONObject.parseObject( resultText );
        String code = Util.null2String( resultJson.getString( "code" ) );
        String data = Util.null2String( resultJson.getString( "data" ) );
        if ("200".equals( code )) {
            rs.executeUpdate( "update uf_xshttz set czzt='1' where id=?", mainid );
            SunTaiUtil.saveCallInterLog( "销售合同新增接口", data, mainid, userid );
            addSuccessCount++;
        } else if ("555".equals( code )) {//变更销售合同
            JSONObject jsonData1 = new JSONObject();
            jsonData1.put( "companyCode", "01-0070-202202-001" );
            jsonData1.put( "contractCode", Util.null2String( rs.getString( "htbh" ) ) );
            jsonData1.put( "changedAmount", Util.null2String( rs.getString( "htje" ) ) );
            jsonData1.put( "changedCurrency", Util.null2String( rs.getString( "bzwb" ) ) );
            jsonData1.put( "changedExcRate", Util.null2String( rs.getString( "hl" ) ) );
            jsonData1.put( "effectiveDate", Util.null2String( rs.getString( "kssj" ) ) );
            jsonData1.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );
            jsonData.put( "contractStatus", Util.null2String( rs.getString( "htzt" ) ) );
            jsonData1.put( "tenantCode", "ZDKST" );
            jsonData1.put( "status", "rkz");
            String resultText1 = InvokeHelper.addChangeSellcont( jsonData1.toJSONString() );
            JSONObject resultJson1 = JSONObject.parseObject( resultText1 );
            String code1 = Util.null2String( resultJson1.getString( "code" ) );
            String data1 = Util.null2String( resultJson1.getString( "data" ) );
            if ("200".equals( code1 )) {
                rs.executeUpdate( "update uf_xshttz set czzt='2' where id=?", mainid );
                changeSuccessCount++;
            } else {
                changeFailureCount++;
            }
            SunTaiUtil.saveCallInterLog( "销售合同变更新增接口", data1, mainid, userid );
        } else {
            SunTaiUtil.saveCallInterLog( "销售合同新增接口", data, mainid, userid );
            addFailureCount++;
        }

    }
    returnJson.put( "counts", counts );
    returnJson.put( "addSuccessCount", addSuccessCount );
    returnJson.put( "addFailureCount", addFailureCount );
    returnJson.put( "changeSuccessCount", changeSuccessCount );
    returnJson.put( "changeFailureCount", changeFailureCount );
    out.print( returnJson.toJSONString() );
%>
